package com.fulu.jvm;

/**
 * 模拟指令重排产生的问题
 */
public class Singleton {
    private static Singleton instance;

    private Singleton() {
        // 模拟耗时初始化
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}
